<?php
$path = dirname(__FILE__);
require_once $path.'/ISms.php';
require_once $path.'/yimei/sms.lib.php';
require_once $path.'/yimei/nusoap.php';
/**
 * Copyright (c) 2012, Jiehun.com.cn Inc. All Rights Reserved
 * @author huqingping@jiehun.com.cn
 * @date 2012-2-11
 * @version 1.0
 * @brief
 *
 **/
final class YimeiSms extends ISms
{
	var $url;
	var $serialNumber;
	var $passwd;
	var $sessionKey;

	var $connect_timeout=10;
	var $response_timeout=10;
	var $suffix;

	var $client;

	var $logFunc;

	function __construct($conf)
	{
		$this->url = $conf['url'];
		$this->serialNumber = $conf['serial_number'];
		$this->passwd = $conf['passwd'];
		$this->sessionKey = $conf['session_key'];

		$this->connect_timeout = $conf['connect_timeout'];
		$this->response_timeout = $conf['response_timeout'];
		$this->suffix = $conf['msg_suffix'];

		$this->encoding = empty($conf['encoding'])?'UTF-8':$conf['encoding'];

		$this->logFunc = (!empty($conf['log_func'])&&is_callable($conf['log_func']))?$conf['log_func']:null;

		$this->client = new SmsClient($this->url,$this->serialNumber,$this->passwd,$this->sessionKey,
		false,false,false,false,$this->connect_timeout,$this->response_timeout);
		$this->client->setOutgoingEncoding($this->encoding);
	}

	function send($msg,$phone)
	{
		if (is_string($phone) || is_numeric($phone)) {
			$phone = array($phone);
		}
		if ($this->logFunc) {
			call_user_func($this->logFunc,"send msg to $phone ".date('Y-m-d H:i:s'));
		}
		global $__HapN_appid;
		if ($__HapN_appid) {
			//在HapN框架的一个特殊支持，不用再HapN下也无影响
			$__HapN_appid ++;
		}
		$msg = $msg.$this->suffix;
		$statusCode = $this->client->sendSMS($phone,$msg);
		Logger::trace('sms'.$statusCode);

		if ($this->logFunc) {
			call_user_func($this->logFunc,$this->client->getDebug());
		}

		if($statusCode != 0 && $this->logFunc) {
			call_user_func($this->logFunc,'sms send result is '.$statusCode);
			return false;
		}
		return $this->getUseCount($msg);
	}

	function getBalance()
	{
		return $this->client->getBalance()*10;
	}

	
}
